using Sustainalytics.CDDB.DataConnector;
using Sustainalytics.Entities;
using Sustainalytics.SharedEntities.Corporate;
using Sustainalytics.Synchronization.Interface;
using System.Collections.Generic;
using System.Diagnostics;

namespace Sustainalytics.ClientDW.Synchronization.SyncReferenceUniv.ReferenceUniverseLogic
{
    public class ReferenceUniverseCddbQuery
    {
        private readonly CorporateDataRepository corporateDataRepository;
        private readonly ILogger logger;
        private readonly BackOfficeAppSettings backOfficeAppSettings;

        public ReferenceUniverseCddbQuery(CorporateDataRepository corporateDataRepository, ILogger logger, BackOfficeAppSettings backOfficeAppSettings)
        {
            this.corporateDataRepository = corporateDataRepository;
            this.logger = logger;
            this.backOfficeAppSettings = backOfficeAppSettings;
            logger.Log($"\nConnection to CDDB trift {backOfficeAppSettings.ThriftServer} establish");
        }

        public IList<ReferenceUniverse> Get(bool retry = true)
        {
            var stopWatch = Stopwatch.StartNew();

            List<ReferenceUniverse> result;
            if (!retry)
                result = corporateDataRepository.GetReferenceUniverses();
            else
            {
                result = Retry.Do(() =>
                       corporateDataRepository.GetReferenceUniverses(),
                       backOfficeAppSettings.WaitRetryDuration, logger, backOfficeAppSettings.RetryNumberOfTimes);
            }

            logger.Log($"Received {result.Count} Reference Universes from Hbase $ ( duration {stopWatch.Elapsed} )");

            return result;
        }
    }
}